package com.ngkj.zmfptback.sys.service.write.impl;

import com.ngkj.zmfptback.sys.bean.RoleEntity;
import com.ngkj.zmfptback.sys.mapper.read.AuthReadMapper;
import com.ngkj.zmfptback.sys.mapper.read.RoleAuthReadMapper;
import com.ngkj.zmfptback.sys.mapper.write.RoleAuthWriteMapper;
import com.ngkj.zmfptback.sys.mapper.write.RoleWriteMapper;
import com.ngkj.zmfptback.sys.mapper.write.UserRoleWriteMapper;
import com.ngkj.zmfptback.sys.service.write.RoleWriteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ChenHN
 * @since 2021/12/22 creation
 **/
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class RoleWriteServiceImpl implements RoleWriteService {

    @Resource
    private RoleWriteMapper roleWriteMapper;

    @Resource
    private RoleAuthWriteMapper roleAuthWriteMapper;

    @Resource
    private RoleAuthReadMapper roleAuthReadMapper;

    @Resource
    private AuthReadMapper authReadMapper;

    @Resource
    private UserRoleWriteMapper userRoleWriteMapper;

    @Override
    public void addRole(RoleEntity roleEntity, String authStr, String menuStr) {
        roleEntity.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //新增角色
        roleWriteMapper.addRole(roleEntity);
        //新增角色对应操作权限
        List<String> list = new ArrayList<>();
        if (!StringUtils.isEmpty(authStr)) {
            String[] addAuths = authStr.split(",");
            for (String addAuth : addAuths) {
                list.add(addAuth);
            }
        }

        //新增角色对应菜单权限
        if (!StringUtils.isEmpty(menuStr)){
            List<String> list1 = new ArrayList<>();
            String[] addMenu = menuStr.split(",");
            for (String menu : addMenu) {
                list1.add(menu);
            }
            List<String> authIds = authReadMapper.getAuthByMenuIds(list1,"MENU");
            list.addAll(authIds);
        }

        roleAuthWriteMapper.addRoleAuth(list, roleEntity.getId());
    }

    @Override
    public void deleteRole(Integer id) {
        roleWriteMapper.deleteRole(id);
        roleAuthWriteMapper.deleteAll(id);
        userRoleWriteMapper.deleteUserRolesByRoleId(id);
    }

    @Override
    public void updateRole(RoleEntity roleEntity, String authStr, String menuStr) {
        roleWriteMapper.updateRole(roleEntity);
        //查询该角色所有权限，不分类
        List<String> roleAuths = roleAuthReadMapper.getRoleAuths(roleEntity.getId(), null);
        List<String> addAuths = new ArrayList<>();
        List<String> interAuths = new ArrayList<>(roleAuths);
        //当前操作权限
        if (!StringUtils.isEmpty(authStr)){
        String[] split = authStr.split(",");
        for (String s : split) {
            addAuths.add(s);
        }
        }

        //当前菜单权限，先将菜单id 转为权限id
        if (!StringUtils.isEmpty(menuStr)){
            List<String> list1 = new ArrayList<>();
            String[] addMenu = menuStr.split(",");
            for (String menu : addMenu) {
                list1.add(menu);
            }
            List<String> authIds = authReadMapper.getAuthByMenuIds(list1,"MENU");
            addAuths.addAll(authIds);
        }

        interAuths.retainAll(addAuths);
        addAuths.removeAll(interAuths);
        if (addAuths.size()>0){
            roleAuthWriteMapper.addRoleAuth(addAuths,roleEntity.getId());
        }
        roleAuths.removeAll(interAuths);
        if (roleAuths.size()>0){
            roleAuthWriteMapper.deleteRoleAuth(roleAuths,roleEntity.getId());
        }
    }
}
